<template>
  <view>
    <!-- 搜索模块 -->
    <search></search>
    <!-- 主体区域 -->
    <view class="m-category">
      <scroll-view scroll-y class="left">
        <view 
          @click="changeActive(index)"
          class="item"
          :class="{ active: index === activeIndex }"
          v-for="(item, index) in leftList"
          :key="item.cat_id">
            {{ item.cat_name }}
          </view>
      </scroll-view>
      <scroll-view :scroll-top="scrollTop" scroll-y class="right">
        <view class="cate" v-for="item in rightList" :key="item.cat_id">
          <view class="title">{{ item.cat_name }}</view>
          <view class="goods-list">
            <navigator :url="`/goodsPkg/goods_list/goods_list?query=${goods.cat_name}&cid=${goods.cat_id}`" class="goods" v-for="goods in item.children" :key="goods.cat_id">
              <image
                :src="goods.cat_icon"
                class="pic"
                mode="aspectFit"
              />
              <view class="name">{{ goods.cat_name }}</view>
            </navigator>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'Category',
  data() {
    return {
      // 在 data 中定义的数据都是响应式的
      // 所谓的响应式就是 vue 帮我们监控对象的变化, 只要变了就自动更新视图, 所以是需要消耗性能的
      // cateList: [],
      leftList: [],
      rightList: [],
      activeIndex: 0,
      scrollTop: 0
    }
  },
  onLoad() {
    this.getCateList()
  },
  methods: {
    changeActive(index) {
      // 改激活的索引实现高亮
      this.activeIndex = index
      // 根据索引找到对应的右侧分类数据
      // console.log(this.cateList[index].children)
      this.rightList = this.cateList[index].children
      // 先将 scrollTop 设置为 -1, 为了让 ScrollView 能监测到滚动数据的变化
      this.scrollTop = -1
      // 数据更新后, 视图的更新是异步的
      this.$nextTick(() => {
        // scroll-view 滚动到顶部
        this.scrollTop = 0
      })
    },
    async getCateList() {
      const { message } = await uni.$u.http.get('/categories')
      // console.log(message)
      // cateList 没有在 data 中定义, 所以它不是响应式的
      this.cateList = message
      // 从大数组中筛选出我们需要的小数组, 就是性能优化了
      // [ { id: '', name }, ... ]
      this.leftList = message.map(item => ({ cat_id: item.cat_id, cat_name: item.cat_name }))

      // console.log('cateList:', this.cateList)
      // console.log('leftList:', this.leftList)
      // 获取到数据后再触发第一个元素被点击的效果
      this.changeActive(0)
    }
  }
};
</script>

<style lang="scss">
.m-category {
  display: flex;
  height: calc(100vh - 20rpx - 34px);
  .left {
    height: 100%;
    width: 182rpx;
    font-size: 28rpx;
    text-align: center;
    background-color: #F3F3F3;
    .item {
      padding: 17rpx 0;
      &.active{
        background: #fff;
        color: #ea4350;
        position: relative;
        &::before{
          content: '';
          position: absolute;
          left: 0;
          width: 4rpx;
          height: 44rpx;
          background: #ea4350;
        }
      }
    }
  }
  .right {
    height: 100%;
    flex: 1;
    .title{
      margin: 18rpx 0 21rpx;
      text-align: center;
    }
  }
}
.goods-list{
  display: flex;
  padding: 0 48rpx;
  justify-content: space-between;
  flex-wrap: wrap;
  .pic{
    width: 120rpx;
    height: 120rpx;
  }
  .name{
    margin-bottom: 30rpx;
    font-size: 21rpx;
    color: #2E2E2E;
    text-align: center;
    line-height: 1;
  }
}
</style>